/*
 * hudong.com Inc.
 * Copyright (c) 2000-2004 All Rights Reserved.
 */
package middlegen.plugins.ibatis.util;

import middlegen.plugins.ibatis.IWalletPlugin;

import org.apache.commons.lang.StringUtils;

public class Util {
    public static String getSimpleJavaType(String type) {
        if (StringUtils.isBlank(type)) {
            return type;
        }

        int index = type.lastIndexOf(".");

        if (index >= 0) {
            return type.substring(index + 1);
        } else {
            return type;
        }
    }

    /**
     * Get a default value for a given java type.
     *
     * @param type
     * @return
     */
    public static String getDefaultValue(String type) {
        if (StringUtils.isBlank(type)) {
            return "null";
        } else if (type.equals(IWalletPlugin.MONEY_CLASS)) {
        	// special case
        	return "0";
        } else if (type.lastIndexOf(".") > 0) {
            return "null";
        } else if (Character.isUpperCase(type.charAt(0))) {
            return "null";
        } else if ("boolean".equals(type)) {
            return "false";
        } else {
            return "0";
        }
    }

    public static boolean isNeedImport(String type) {
        if (StringUtils.isBlank(type)) {
            return false;
        }

        if (type.startsWith("java.lang.")) {
            return false;
        }

        if ((type.indexOf(".") < 0) && Character.isLowerCase(type.charAt(0))) {
            return false;
        }

        return true;
    }

    public static String toUpperCaseWithDash(String name) {
        String newName;

        if (name == null) {
            newName = name;
        } else {
            newName = StringUtils.upperCase(name);

            if (newName != null) {
                newName = newName.replaceAll("_", "-");
            }
        }

        return newName;
    }
}
